using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Helper;
using NCS.Module.UngDung.Library.UngDung;
using NCS.Library;
using NCS.Library.Utilities;

namespace NCS.Module.UngDung.Library.Providers
{
    public class SqlDataProvider : DataAccessProvider
    {
        private string connectionString = string.Empty;

        public string ConnectionString
        {
            get { return this.connectionString; }
            set { this.connectionString = value; }
        }

        public SqlDataProvider(string strConn)
        {
            this.ConnectionString = strConn;
        }

        public override DataTable GetUngDungAvailable()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spUngDung_GetAllAvailable", AppEnv.GetLanguage(), int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetAllUngDung()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spUngDung_GetAll", AppEnv.GetLanguage(), int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetAllUngDungFilter(int iStatus)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spUngDung_GetAllByFilter", AppEnv.GetLanguage(), iStatus, int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override UngDungInfo GetInfo(int id)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "spUngDung_GetInfo", id);
            UngDungInfo obj = (UngDungInfo)ObjectHelper.CreateObject(typeof(UngDungInfo), dr);
            return obj;
        }

        public override int SetIndex(int id, int index)
        {
            return SqlHelper.ExecuteNonQuery(this.ConnectionString, "spUngDung_SetIndex", id, index);
        }

        public override int SetStatus(int id, bool status)
        {
            return SqlHelper.ExecuteNonQuery(this.ConnectionString, "spUngDung_SetStatus", id, status);
        }

        public override int CreateUngDung(UngDungInfo objUngDungInfo)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "spUngDung_Insert",
                                                objUngDungInfo.CapChaID,
                                                objUngDungInfo.TenUngDung,
                                                objUngDungInfo.NoiDung,
                                                objUngDungInfo.TrangThai,
                                                objUngDungInfo.ThuTu,
                                                objUngDungInfo.CreateUserID,
                                                objUngDungInfo.PortalID,
                                                AppEnv.GetLanguage());
        }

        public override int UpdateUngDung(UngDungInfo objUngDungInfo)
        {
            return (int)SqlHelper.ExecuteNonQuery(this.ConnectionString, "spUngDung_Update",
                                               objUngDungInfo.UngDungID,
                                               objUngDungInfo.CapChaID,
                                               objUngDungInfo.TenUngDung,
                                               objUngDungInfo.NoiDung,
                                               objUngDungInfo.TrangThai,
                                               objUngDungInfo.ThuTu,
                                               objUngDungInfo.ModifyUserID,
                                               objUngDungInfo.PortalID,
                                               AppEnv.GetLanguage());
        }

        public override int DeleteUngDung(int id)
        {
            return (int)SqlHelper.ExecuteNonQuery(this.ConnectionString, "spUngDung_Delete", id);
        }

        public override int GetCount()
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "spUngDung_Count", AppEnv.GetLanguage(), int.Parse(AppEnv.PortalId()));
        }

        public override DataTable GetUngDung(int parentId, int portalid, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spGetUngDungByParentIDAndPortalID", parentId, portalid, lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetAllChildByID(int id, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "sp_UngDungParentChildren", id, lang, int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
    }
}
